﻿using UnityEngine;
using System.Collections;

public class GameUI : MonoBehaviour {
	//main screen
	static public GameUI instance;

	public GUIText guiTitle;
	public GUIText guiMessage;
	public GUIText guiTimer;
	public GUIText guiScore1;
	public GUIText guiScore2;
	public GUIText guiPlayer1;
	public GUIText guiPlayer2;
	
	private Rect endGameRect;
	private Rect singleGameRect;
	private Rect createGameRect;
	private Rect joinGameRect;
	private Rect backRect;
	private Rect fieldRect;

	private bool bGameStarted = false;
	
	private float gameTimer = 0;

	// Use this for initialization
	void Start () {
		instance = this;
		guiMessage.enabled = false;
		guiTimer.enabled = false;
		guiScore1.enabled = false;
		guiScore2.enabled = false;

		endGameRect = new Rect ( Screen.width / 2 - 50, 10, 100, 50 );
		singleGameRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 - 150, 200, 90);
		createGameRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 - 50, 200, 90);
		joinGameRect = new Rect (Screen.width / 2 - 100, Screen.height / 2 + 50, 200, 90);
		
		backRect = new Rect( 10, 10, 100, 50 );
		fieldRect = new Rect( 10, 60, 100, 50 );

		// hide game over, results
		GameEvents.GameStart += OnGameStart;
		GameEvents.GameOver += OnGameOver;
	}
	
	void OnDestroy() {
		GameEvents.GameStart -= OnGameStart;
		GameEvents.GameOver -= OnGameOver;
	}
	
	// Update is called once per frame
	void Update () {
		/*if (!bGameStarted) {
			if (Input.GetMouseButtonDown (0))
				GameEvents.TriggerGameStart ();
		}*/
		
		if (bGameStarted) {
			gameTimer += Time.deltaTime;
			int min = Mathf.FloorToInt(gameTimer) / 60;
			int secs = Mathf.FloorToInt(gameTimer) % 60;
			guiTimer.text = string.Format("{0}:{1}", min, secs);
		}
	}

	void OnGUI() {
		if (bGameStarted) {
			if (GUI.Button(endGameRect, "End Game")) {
				showMessage( "Game Cancelled!" );
				GameEvents.TriggerGameOver();
			}
			return;
		}
		
		if (GUI.Button(singleGameRect, "Local Game")) {
			//start solo game
			GameManager.gameType = GameType.Local;
			GameEvents.TriggerGameStart();
		}

		if (GUI.Button (createGameRect, "Host Game")) {
			GameManager.gameType = GameType.Host;
			GameEvents.TriggerGameStart();
		}
		
		if (GUI.Button (joinGameRect, "Join Game")) {
			GameManager.gameType = GameType.Guest;
			GameEvents.TriggerGameStart ();
		}
		
		if (GUI.Button (backRect, "Pool")) {
			Application.LoadLevel("Pool");
		}
		
		if (GUI.Button(fieldRect, "Board")) {
			Application.LoadLevel("Board");
		}
	}

	void OnGameStart() {
		Debug.Log ("GameUI.OnGameStart");
		bGameStarted = true;
		guiMessage.enabled = false;
		guiTimer.enabled = true;
		guiTitle.enabled = false;
		guiScore1.enabled = true;
		guiScore2.enabled = true;
		//enabled = false;
	}

	void OnGameOver() {
		guiMessage.enabled = true;
		guiTimer.enabled = true;
		guiTitle.enabled = true;
		bGameStarted = false;
		//enabled = true;
	}

	public void hideMessage() {
		guiMessage.enabled = false;
	}

	public void showMessage( string text ) {
		guiMessage.text = text;
		guiMessage.enabled = true;
	}

	public void showVictoryMessage( PlayerColor color ) {
		if (color == PlayerColor.Red) {
			guiMessage.text = "RED wins";
			guiMessage.color = Color.red;
		}
		else {
			guiMessage.text = "BLUE wins";
			guiMessage.color = Color.blue;
		}

		Debug.Log ("GameUI.showVictoryMessage: text=" + guiMessage.text);
		guiMessage.enabled = true;
	}

	public void showPlayers( string player1, string player2 ) {
		guiPlayer1.text = player1;
		guiPlayer2.text = player2;
	}
	
	public void showScore( int score1, int score2 ) {
		guiScore1.text = string.Format("Score: {0}", score1);
		guiScore2.text = string.Format("Score: {0}", score2);
	}
}
